package com.skeleton.server.user.control;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.db.dto.UserLoginHistoryDto;
import com.skeleton.db.dto.UserinfoDto;
import com.skeleton.server.user.service.IUserService;
import com.skeleton.server.user.util.UserConstants;
import com.skeleton.utils.SystemTools;

@Controller
@RequestMapping("user/")
public class UserControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private IUserService iUserService;

	@RequestMapping("ajaxLogin")
	@ResponseBody
	public Map<String, Object> ajaxLogin(HttpServletRequest request, HttpSession session) {
		logger.info("-------user---ajaxLogin-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String loginname = request.getParameter("loginname");
		String password = request.getParameter("password");

		UserinfoDto uDto = new UserinfoDto();
		uDto.setUserLoginname(loginname);
		uDto.setUserPassword(password);

		UserLoginHistoryDto ulhDto = new UserLoginHistoryDto();
		ulhDto.setUserLoginname(loginname);
		ulhDto.setHistoryLoginDttm(SystemTools.convertCurrentDTTMtoInt());
		ulhDto.setHistoryLoginIp(request.getRemoteAddr());

		ResultDto rd = iUserService.doLogin(uDto, ulhDto);
		if (rd.isSuccess()) {
			UserinfoDto ud = (UserinfoDto) rd.getFirstDto("UserinfoDto");
			session.setAttribute(UserConstants.LOGINUSER_SESSION_NAME, ud);
			map.put("loginname", loginname);
			map.put("result", UserConstants.AJAX_SUCCESS);
		} else {
			map.put("result", UserConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("ajaxLogout")
	@ResponseBody
	public Map<String, Object> ajaxLogout(HttpServletRequest request, HttpSession session) {
		logger.info("-------user---ajaxLogout-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		session.removeAttribute(UserConstants.LOGINUSER_SESSION_NAME);

		map.put("result", UserConstants.AJAX_SUCCESS);
		return map;
	}

	@RequestMapping("ajaxRegist")
	@ResponseBody
	public Map<String, Object> ajaxRegist(HttpServletRequest request) {
		logger.info("-------user---ajaxRegist-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String loginname = request.getParameter("loginname");
		String password = request.getParameter("password");

		UserinfoDto uDto = new UserinfoDto();
		uDto.setUserLoginname(loginname);
		uDto.setUserPassword(password);
		uDto.setUserCreateDttm(SystemTools.convertCurrentDTTMtoInt());
		uDto.setUserCreateIp(request.getRemoteAddr());
		uDto.setUserType(UserConstants.USER_TYPE_NORMAL);
		uDto.setUserStatus(UserConstants.USER_STATUS_NORMAL);

		ResultDto rd = iUserService.doSimpleRegist(uDto);
		if (rd.isSuccess()) {
			map.put("result", UserConstants.AJAX_SUCCESS);
		} else {
			map.put("result", UserConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("getUserById")
	@ResponseBody
	public Map<String, Object> getUserById(HttpServletRequest request) {
		logger.info("-------user---getUserById-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String loginname = request.getParameter("loginname");

		UserinfoDto uDto = new UserinfoDto();
		uDto.setUserLoginname(loginname);

		ResultDto rd = iUserService.getUserinfoByLoginname(uDto);
		if (rd.isSuccess()) {
			UserinfoDto ud = (UserinfoDto) rd.getFirstDto("UserinfoDto");
			map.put("ud", ud);
			map.put("result", UserConstants.AJAX_SUCCESS);
		} else {
			map.put("result", UserConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

}
